package com.sheep.spark.job;

import com.sheep.spark.util.ConfSupport;
import com.sheep.spark.util.SparkSupport;
import org.apache.log4j.Logger;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 16-6-2
 * Time: 上午10:29
 * To change this template use File | Settings | File Templates.
 */
abstract public class FileStreamJob implements Serializable {
    private static final Logger logger = Logger.getLogger(FileStreamJob.class);
    protected JavaStreamingContext javaStreamingContext = null;
    protected String filePath = "";

    public FileStreamJob(String filePath) {
        this.filePath = filePath;
        try {
            init();
        } catch (Exception e) {
            logger.error(e);
        }
    }

    private void init() throws Exception {
        run(SparkSupport.getNewJavaStreamingContext("fileStreamJob").textFileStream(filePath));
        SparkSupport.getJavaStreamingContext().start();
        SparkSupport.getJavaStreamingContext().awaitTermination();
    }

    abstract public void run(JavaDStream<String> lines);
}

